package top.gelonggld.model;

import top.gelonggld.constants.PType;
import top.gelonggld.constants.Rex;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gelon on 2017/5/27.
 * Note是代码的中的注解
 */
public class Note extends BaseParagraph{

    String noteName;
    List<NotePropety> notePropeties;
    String defaultPropeties;

    public Note(int start, int end, String paragraphStr, int type,String noteName) {
        super(start, end, paragraphStr, type);
        this.noteName = noteName;
    }


    public String getNoteName() {
        return noteName;
    }

    public Note setNoteName(String noteName) {
        this.noteName = noteName;
        return this;
    }

    public List<NotePropety> getNotePropeties() {
        return notePropeties;
    }

    public Note setNotePropeties(List<NotePropety> notePropeties) {
        this.notePropeties = notePropeties;
        return this;
    }

    @Override
    public void onPushDate(String paragraphStr) {
        Matcher matcher = Pattern.compile(Rex.NOTE).matcher(paragraphStr);
        matcher.find();
        NormalParagraph noteProParagraph = new NormalParagraph(matcher.start(3) + start,matcher.end(3) + end,
                matcher.group(3), PType.NORMAL_PAAGRAPH);
        notePropeties = parNotePropeties(noteProParagraph);

    }

    private List<NotePropety> parNotePropeties(NormalParagraph noteProParagraph) {
        List<NotePropety> noteP = new ArrayList<>();
        if(noteProParagraph.paragraphStr == null) { return null;}
        Matcher matcher = Pattern.compile(Rex.NOTE_PROPERTY).matcher(noteProParagraph.paragraphStr);
        while (matcher.find()){
            noteP.add(new NotePropety(matcher.start()+noteProParagraph.start,matcher.end() + noteProParagraph.start,matcher.group(),
                    PType.NOTE_PROPETY,matcher.group(1),matcher.group(2)));
        }
        if(noteP.size() == 0){
            defaultPropeties = noteProParagraph.paragraphStr;
        }
        return noteP ;
    }

    @Override
    public void initDate() {
        notePropeties = new ArrayList<>();
    }

    public String getDefaultPropeties() {
        return defaultPropeties;
    }
}
